<template>
  <div>
    <quill-editor  v-model="content"
                   :options="editorOption"
                   ref="myQuillEditor"
                   @blur="onEditorBlur($event)"
                   @focus="onEditorFocus($event)"
                   @change="onEditorChange($event)"
    @rea="onEditorReady">
    </quill-editor>
    <el-upload
      class="avatar-uploader"
      :action="articleImgUrl"
      :headers="headers"
      name="picture"
      :show-file-list="false"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :before-upload="beforeUpload">
    </el-upload>
  </div>
</template>

<script>
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import { getToken } from "@/utils/auth";
import Quill from 'quill'

export default {
  name: "quill_Editor",
  components: { quillEditor },
  props: {
    /* 编辑器的内容 */
    value: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      articleImgUrl: process.env.VUE_APP_BASE_API+"/construction/picInfo/uploadPicture",
      headers: {
        Authorization: "Bearer " + getToken()
      },
      Quill: null,
      content: null,
      editorOption: {
        modules: {
          toolbar: {
            container:[
              ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
              ["blockquote", "code-block"], // 引用  代码块
              [{ header: 1 }, { header: 2 }], // 1、2 级标题
              [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
              [{ script: "sub" }, { script: "super" }], // 上标/下标
              [{ indent: "-1" }, { indent: "+1" }], // 缩进
              // [{'direction': 'rtl'}],                         // 文本方向
              [{ size: ["small", false, "large", "huge"] }], // 字体大小
              [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
              [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
              [{ font: [] }], // 字体种类
              [{ align: [] }], // 对齐方式
              ["clean"], // 清除文本格式
              ["link", "image", "video"], // 链接、图片、视频
            ],//工具菜单栏配置
            handlers: {
              'image': function (value) {    //触发图片上传的时候返回的信息
                if (value) {
                  // upload点击上传事件
                  document.querySelector('.avatar-uploader input').click()
                } else {
                  this.quill.format('image', false)
                }
              }
            },
          },
        },
        placeholder: '请在这里添加产品描述', //提示
        readyOnly: false, //是否只读
        theme: 'snow', //主题 snow/bubble
        syntax: true, //语法检测
      }
    }
  },
  created() {
    // console.log(this.value)
    // this.content = this.value;
    // console.log(this.content)
  },
  watch:{
    value: {
      handler(val) {
        if (val !== this.content) {
          this.content = val === null ? "" : val;
          if (this.Quill) {
            this.Quill.pasteHTML(this.content);
          }
        }
      },
      immediate: true,
    }
  },
  methods: {
    // 失去焦点
    onEditorBlur(editor) {
    },
    // 获得焦点
    onEditorFocus(editor) {
    },
    // 开始
    onEditorReady(editor) {
    },
    // 值发生变化
    onEditorChange(editor) {
      this.content = editor.html;
      this.$emit('getChildrenStatus', this.content) // 将改变通知父组件(保证父子组件数据一致)
      // this.$emit('getChildrenStatusa', this.content) // 将改变通知父组件(保证父子组件数据一致)
      // this.$emit('getChildrenStatusi', this.content) // 将改变通知父组件(保证父子组件数据一致)
    },
    uploadSuccess(res) {
      var dt = res
      let quill = this.$refs.myQuillEditor.quill
      // 如果上传成功
      // if (dt.msg === this.GLB.IMAGE_ADD_SUCCESS && dt.msg !== null) {
      //返回的图片路径是否为空
      if (dt.msg !== null) {
        // 获取光标所在位置
        let length = quill.getSelection().index
        // 插入图片  dt.msg为服务器返回的图片地址
        quill.insertEmbed(length, 'image', dt.msg)
        // 调整光标到最后
        quill.setSelection(length + 1)
      } else {
        this.$message.error('图片插入失败')
      }
      // loading加载隐藏
      this.quillUpdateImg = false
    },
    uploadError(){
      this.$message.error('图片上传失败')
    },
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 500;
      console.log("isJPG:"+isJPG+"||isPNG:"+isPNG+"||isLt2M:"+isLt2M)
      if (!isJPG && !isPNG) {
        this.$message.error('上传图片只能是 JPG或PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 500MB!');
      }
      return isJPG || isPNG && isLt2M;
    }
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill;
    }
  },
}
</script>

<style scoped>

</style>
